package com.huanwei.mini.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.huanwei.mini.R;
import com.huanwei.mini.http.ML;
import com.huanwei.mini.interfaces.OnItemClickListener;
import com.huanwei.login_regist.Three_Activity;

import java.io.Serializable;
import java.util.List;


/**
 * Created by HongJay on 2016/9/13.
 */
public class Recycle_Baseadapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {


    public Recycle_Baseadapter(List<ML.SdataBean.FirstBean> ccList) {
        this.ccList = ccList;
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //        头部

        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.tab_rx, parent, false);
        OneViewHolder viewHolder = new OneViewHolder(view);
        return viewHolder;


    }

    List<ML.SdataBean.FirstBean> ccList;
    Context context;

    public void adddata(Context context) {

        this.context = context;
    }

    List<ML.SdataBean.FirstBean.SecondBean> second;
    @SuppressLint("WrongConstant")
    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder,final int position) {
        GridLayoutManager gridLayoutManager = new GridLayoutManager(context, 4);
        gridLayoutManager.setAutoMeasureEnabled(true);
        ML.SdataBean.FirstBean dataEntity = ccList.get(position);
        String menuname = dataEntity.getMenuname();
        ((OneViewHolder) holder).tv.setText(menuname);
         second = dataEntity.getSecond();
        if (second != null) {
            Recycle_Gridadapter demoAdapter = new Recycle_Gridadapter(second,position);
            ((OneViewHolder) holder).recycle.setLayoutManager(gridLayoutManager);
            ((OneViewHolder) holder).recycle.setAdapter(demoAdapter);
            demoAdapter.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(View view, int position2,int positionbase) {
                    //耳机目录的点击事件
                    ML.SdataBean.FirstBean.SecondBean secondBean = ccList.get(positionbase).getSecond().get(position2);

                    List<ML.SdataBean.FirstBean.SecondBean.ThirdBean> third = secondBean.getThird();
                    if (third!=null) {
                        Intent intent = new Intent(context, Three_Activity.class);
                        intent.putExtra("words",(Serializable) third);
                        intent.putExtra("words2",""+secondBean.getMenuname() );
                        context. startActivity(intent);
                    } else {
                        Toast.makeText(context, "没第三极", Toast.LENGTH_SHORT).show();
                    }


                }
            });


        }


    }


    @Override
    public int getItemCount() {
        return ccList.size();
    }

    public class OneViewHolder extends RecyclerView.ViewHolder {

        public RecyclerView recycle;
        TextView tv;

        public OneViewHolder(View itemView) {
            super(itemView);


            tv = (TextView) itemView.findViewById(R.id.tv);
            recycle = (RecyclerView) itemView.findViewById(R.id.rx);

        }
    }


}
